package linkedlist;



public class MySingleList implements IList{

    //节点内部类
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;


    }

    public void add(ListNode listNode){
        if (head == null){
            head = listNode;
            return;
        }
        head.next = listNode;
        return;
    }
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;

    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode tmp = head;
        if (tmp == null){
            head = node;
        }else{
            while(tmp.next != null){
                tmp = tmp.next;
            }
            tmp.next = node;
        }



    }

    @Override
    public void addIndex(int index, int data) {
        //校验下标正确性
        if(index > size() || index < 0){
            //此处也可以自定义异常
            System.out.println("请输入合法下标!!!");
            return;
        }
        if (index == 0){
            //下标为零头插法
            addFirst(data);
        }else if (index == size()){
            //下标为Size尾插法
            addLast(data);
        }else{
            ListNode tmp = head;
            ListNode node = new ListNode(data);
            for (int i = 0; i < index-1; i++){
                tmp = tmp.next;
            }
            node.next = tmp.next;//存放节点后next的节点
            tmp.next = node;//插入节点
            //将插入节点的next改为原先所处此位置的节点

        }





    }
    @Override
    public boolean contains(int key) {
        ListNode tmp = head;
        while(tmp != null){
            if (tmp.val == key){
                return true;
            }
            tmp = tmp.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (head == null){
            //如果链表是空的，直接返回
            return;
        }
        if (head.val == key){
            //如果头结点val等于key，将头结点指向它的next位置
            head = head.next;
            return;
        }
        ListNode pre = findPre(key);
        if (pre == null){
            System.out.println("没找到");
            return;
        }
        ListNode del = pre.next;
        pre.next = del.next;
    }
    private ListNode findPre(int key){
        ListNode tmp = head;
        while(tmp.next != null){
            if(tmp.next.val == key){
                return tmp;
            }
            tmp = tmp.next;
        }
        return null;

    }

    @Override
    public void removeAllKey(int key) {
        if (head == null){
            //空链表直接返回
            return;
        }
        ListNode pre = head;
        ListNode tmp = head.next;

        while(tmp != null){
            if (tmp.val == key){
                pre.next = tmp.next;
                tmp = tmp.next;
            }else{
                pre = tmp;
                tmp = tmp.next;
            }
        }
        if (head.val == key){
            head = head.next;
            return;
        }


    }

    @Override
    public int size() {
        int count = 0;
        ListNode tmp = head;
        while(tmp != null){
            count++;
            tmp = tmp.next;
        }
        return count;
    }

    @Override
    public void clear() {
//        head = null;
        ListNode tmp = head;
        ListNode next = null;
        while(tmp != null){
            next = tmp.next;
            tmp.val = 0;
            tmp.next = null;
            tmp = next;
        }
        head = null;
    }

    @Override
    public void display() {
        ListNode tmp = head;
        while(tmp != null){
            System.out.print(tmp.val+" ");
            tmp = tmp.next;
        }
        System.out.println();

    }

    //反转链表
    public void reverseList(){
        if (head == null){//空链表
            return;
        }
        if (head.next == null){//只有一个节点
            return;
        }
        ListNode tmp = head.next;
        head.next = null;
        while(tmp != null){
            ListNode after = tmp.next;
            tmp.next = head;
            head = tmp;
            tmp = after;
        }
    }

    //找到中间节点（传统遍历）
    public ListNode middleNode() {
        if (head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode tmp = head;
        int size = size();
        tmp = head;
        for(int i = 0; i < size/2 ;i++){
            tmp = tmp.next;

        }
        return tmp;

    }

    //找到中间节点（双指针）
    public ListNode middleNode2() {
        if(head.next == null){
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
//        while(fast != null && fast.next != null){
//            fast = fast.next.next;
//            slow = slow.next;
//        }
        while(true){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == null || fast.next == null){
                break;
            }
        }
        return slow;
    }

    //返回倒数第k个节点（计算len-k）
    public ListNode kthToLast(int k) {
        ListNode tmp = head;
        int len = 0;
        while(tmp != null){
            tmp = tmp.next;
            len += 1;
        }
        int step = len-k;
        tmp = head;
        for(int i = 0; i<step; i++){
            tmp = tmp.next;

        }
        return tmp;

    }

    //返回倒数第k个节点（双指针版本）,快指针先走k-1步,然后快慢同时前进,当快指针走到表尾时,慢指针所指向的位置就是倒数第k个节点的位置
    public ListNode kthToLast2(int k) {
        if (k <= 0 || head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        int steps = k - 1;
        while (steps != 0){
            fast = fast.next;
            if (fast == null){//处理k越界问题
                return null;
            }
            steps--;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;

        }
        return slow;
    }

    //判断回文
    public boolean isPalindrome() {
        if (head == null || head.next == null){
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
//        while(fast != null && fast.next != null){
//            fast = fast.next.next;
//            slow = slow.next;
//        }
        //1.找到中间位置
        while(true){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == null || fast.next == null){
                break;
            }
        }
        // 2.翻转
        ListNode tmp = slow.next;
        while(tmp != null){
            ListNode tmpNext = tmp.next;
            tmp.next = slow;
            slow = tmp;
            tmp = tmpNext;
        }
        // 3.从前到后，从后往前
        while(head != slow){
            if (head.val != slow.val){
                return false;
            }
            if (head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;

    }


    //以k为分割点，将val小于k的节点放于前，大于k的放在后，并保持原来的相对顺序
    public ListNode partition(int k) {
        // 如果为空直接返回空
        if(head == null){
            return null;
        }
        //定义前半段的头和尾巴
        ListNode bs = null;
        ListNode be = null;
        //定义后半段的头和尾巴
        ListNode as = null;
        ListNode ae = null;

        ListNode tmp = head;
        while(tmp != null){
            if(tmp.val < k){
                if(bs == null){//放入第一个数据时
                    bs = tmp;
                    be = tmp;
                }else{
                    be.next = tmp;
                    be = be.next;
                }
            }else{
                if(as == null){//放入第一个数据时
                    as = tmp;
                    ae = tmp;
                }else{
                    ae.next = tmp;
                    ae = ae.next;
                }

            }
            tmp = tmp.next;
        }
        //第一个区间没数据
        if(bs == null){
            return as;
        }
        //第一个区间有数据
        be.next = as;
        if(as != null){//结尾要置空，否则会产生越界
            ae.next = null;
        }
        return bs;
    }

    // 找到两个链表第一个相交的节点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }
        ListNode pl = headA;
        ListNode ps = headB;
        int lenA = 0,lenB = 0;
        while(pl != null){
            lenA++;
            pl = pl.next;
        }
        while(ps != null){
            lenB++;
            ps = ps.next;
        }
        if(lenA < lenB){
            pl = headB;
            ps = headA;
        }else{
            pl = headA;
            ps = headB;
        }
        int steps = Math.abs(lenA - lenB);
        while(steps != 0){
            pl = pl.next;
            steps--;
        }
        steps = Math.min(lenA,lenB);
        while(steps != 0){
            if(pl == ps){
                return ps;
            }
            pl = pl.next;
            ps = ps.next;
        }
        return null;
    }

    //判断是否有环
    public boolean hasCycle() {
        //定义快慢指针
        ListNode fast = head;
        ListNode slow = head;
        while(true){
            if(fast == null || fast.next == null){
                return false;
            }
            //快指针一次走两步,慢指针一次走一步,如果有环,那么两者一定会相遇
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                return true;
            }

        }
        //链表如果没环则一定会跳出循环
    }

    //返回环的入口节点
    public ListNode detectCycle() {

        //定义快慢指针
        ListNode fast = head;
        ListNode slow = head;
        while(true){
            if(fast == null || fast.next == null){
                return null;
            }
            //快指针一次走两步,慢指针一次走一步,如果有环,那么两者一定会相遇
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }

        }
        fast = head;
        while(slow != fast){
            slow = slow.next;
            fast = fast.next;
        }
        //链表如果没环则一定会跳出循环
        return fast;
    }



}
